<?php

namespace app\home\model;

use Redis;
use Think\Db;
use think\Model;

class Tradelogold extends Model
{
    protected $tableName = 'tradelog';                    /*订单记录表*/
    protected $table_user_props_info = 'user_props_info'; /*道具记录表*/
    protected $table_accounts = 'accounts_info';          /*用户信息表*/
    protected $table_user_game_info = 'user_game_info';     /* 游戏炮倍 */

    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect('db_config2')->table($this->table_accounts);
        return $accounts_info;
    }
    protected function Db_connect_user_game_info()
    {
        $accounts_info = Db::connect('db_config2')->table($this->table_user_game_info);
        return $accounts_info;
    }

    protected function Db_connect_props()
    {
        $props = Db::connect('db_config2')->table($this->table_user_props_info);
        return $props;
    }

    /**
     * 发送数据 添加道具过期时间
     * @author zhangzhiyuan
     * @version 2017-09-20  0-道具 1-数量 2-过期时间
     * @return array
     */
    function curl_go_data($data, $UserID, $ExpireTime)
    {
        $prop_data = [];
        if (count($data) == count($data, 1)) {
            if ($ExpireTime > 0) {
                $res = $this->Db_connect_props()->where('PropID', $data[0])->where('UserID', $UserID)->find();
                if ($res) {
                    if ($res['ExpireTime'] == '2099-01-01 00:00:00') {
                        $expireTime_after = 0;
                    } else {
                        $expireTime_after = strtotime($res['ExpireTime']) + $ExpireTime * 24 * 3600;
                    }
                } else {
                    $expireTime_after = (time() + $ExpireTime * 24 * 3600);
                }
            } else {
                $expireTime_after = 0;
            }
            $data[2] = $expireTime_after;
            $prop_data = $data;
        } else {
            foreach ($data as $k => $v) {
                // 判断过期时间
                if ($ExpireTime > 0) {
                    $res = $this->Db_connect_props()->where('PropID', $v[0])->where('UserID', $UserID)->find();
                    // 如果道具存在并且是永久
                    if ($res) {
                        if ($res['ExpireTime'] == '2099-01-01 00:00:00') {
                            $expireTime_after = 0;
                        } else {
                            $expireTime_after = strtotime($res['ExpireTime']) + $ExpireTime * 24 * 3600;
                        }
                    } else {
                        $expireTime_after = (time() + $ExpireTime * 24 * 3600);
                    }
                } else {
                    $expireTime_after = 0;
                }
                $v[2] = $expireTime_after;
                $prop_data[] = $v;
            }
        }
        return $prop_data;
    }


    /**
     * Aristocratic_bag  礼包道具背包 操作
     * @author zhangzhiyuan
     * @version 2017-09-20  Type:1-金币 2-钻石 3-道具 4-首冲礼包 5-月卡
     * @param string   0 、金币  1、钻石
     * @return true
     */
    function Aristocratic_bag($data, $UserID, $type)
    {
        foreach ($data as $k => $v) {
            if ($v[0] == 0) {
                $re = $this->Db_connect_accounts_info()->where('UserID', $UserID)->setInc('Score', $v[1]);
                if (!$re) {
                    file_put_contents('gift_score_log.txt', "礼包金币记录记录失败\r\n", FILE_APPEND);
                }
            } else if ($v[0] == 1) {
                $re = $this->Db_connect_accounts_info()->where('UserID', $UserID)->setInc('Diamond', $v[1]);
                if (!$re) {
                    file_put_contents('gift_Diamond_log.txt', "礼包钻石记录记录失败\r\n", FILE_APPEND);
                }
            } else if ($v[0] == 2) {
                $re = $this->Db_connect_accounts_info()->where('UserID', $UserID)->setInc('Lottery', $v[1]);
                if (!$re) {
                    file_put_contents('gift_lottery_log.txt', "礼包鱼券记录记录失败\r\n", FILE_APPEND);
                }
            } else {
                $this->Gift_props($UserID, $v[0], $v[1], $type);
            }
        }
    }
    /*活动道具*/
    function Gift_props($UserID, $PropID, $Counts, $type = 1)
    {
        $data['UserID'] = $UserID;
        $data['PropID'] = $PropID;
        $data['Counts'] = $Counts;
        $data['Type'] = $type;
        $data['InserTime'] = date('Y-m-d H:i:s');
        $res = $this->Db_connect_props()->where('PropID', $PropID)->where('UserID', $UserID)->find();
        if ($res) {

            // 如果原来不是永久 就把其更新为永久
            $this->Db_connect_props()->where('PropID', $PropID)->where('UserID', $UserID)->update(['ExpireTime' => '2099-01-01 00:00:00']);
            $re = $this->Db_connect_props()->where('PropID', $PropID)->where('UserID', $UserID)->setInc('Counts', $Counts);
            if ($re) return true;
        } else {
            $re = $this->Db_connect_props()->insert($data);
            if ($re) return true;
        }
    }

    /**
     * 一个玩家只能买一次的计费点
     */
    public function Check_Forever_Limited_Purchase($user_id, $Billing_point)
    {
        $arr = config('CHECK_FOREVER_LIMITED_PURCHASE_BAGS');
        $map = [];
        if (in_array($Billing_point, $arr)) {
            $map['userid'] = $user_id;
            $map['goods_id'] = $Billing_point;
            $map['status'] = 1;
            $qpaccount_old_db = Db::connect('db_config2');
            $count = $qpaccount_old_db->table('tradelog')->where($map)
                ->count();
            if ($count >= 1) {
                return true;
            }
        }

        return false;
    }


    /**
     * 游戏服务器
     * @author
     * @version 2019-03-18
     * @param string  向游戏服务器发送充值消息 (dataArray [[]] 数据包)
     * @return string   success
     */
    public function get_curl_type($UserID, $Type, $dataArray = array(), $total_price, $RechargeStock = 0, $goods_id = 0)
    {
        $arr = $this->Db_connect_accounts_info()->field('Score,Diamond,TotalRecharge,VipLevel,LoginServer')->where('UserID', $UserID)->find();
        $Cannon = $this->Db_connect_user_game_info()->field('Cannon')->where('UserID', $UserID)->find();
        $data = [
            'UserID' => $UserID,
            'Type' => $Type,
            'Score' => (int)$arr['Score'],
            'Recharge' => $arr['TotalRecharge'] + $total_price,
            'VipLevel' => $arr['VipLevel'],
            'Diamond' => $arr['Diamond'],
            'Cannon' => $Cannon['Cannon'],
            'RechargeStock' => (int)$RechargeStock,
            'Award' => $dataArray,
            'GoodsID' => $goods_id,
        ];
        $url_param = json_encode($data);
        $url = getServerUrl($arr['LoginServer']) . '?id=1&data=' . $url_param;

        /*正式*/
        $time = date('Y-m-d H:i:s', time());
        file_put_contents('http_log_new.txt', '请求：' . $url . "发送时间:" . $time . "\r\n", FILE_APPEND);
        $res = curl_get($url);
        return $res;
    }
}
